HAL SPI migration: List of applicable concepts

This section provides information about the HAL SPI applicable architecture concepts.

Concepts for SPI

Concept

Description

Separation of initialization and configuration functions

The initialization has been split into separate init and config functions:

  • HAL_SPI_Init(): initializes the handle and links it to an SPI instance.

  • HAL_SPI_SetConfig(): sets the configuration of the SPI peripheral.

Removal and conversion of init structure to configuration structure

The init structure has been removed and moved to the HAL_SPI_SetConfig() function.

Renaming of the handle structure

The handle structure has been renamed to hal_spi_handle_t.

Renaming unitary init structures

The SPI_InitTypeDef structure has been replaced by hal_spi_config_t.

Remove MspInit and MspDeInit callbacks

The functions hal_spi_MspInit() and hal_spi_MspDeInit() are no longer available and have been replaced by a user sequence to initialize and configure the SPI.

Unitary configuration APIs

New APIs have been introduced to get and set SPI features.

Dedicated DMA link APIs replace generic __HAL_LINKDMA macro

The __HAL_LINKDMA macro is no longer provided and new functions HAL_SPI_SetTxDMA() and HAL_SPI_SetRxDMA() are provided to link an SPI HAL handle to a DMA HAL handle.

Introduction of HAL user data set/get APIs

Two functions to set and get user data are available: HAL_SPI_SetUserData() and HAL_SPI_GetUserData().

Individual callback registration APIs replace global register and unregister functions

The callback registration is handled by specific functions for each SPI event. These functions are used to register callback functions for different SPI events.

HAL implementation with LL inline functions

HAL SPI APIs now rely on LL APIs rather than performing direct register access.

Define statements and enumerations redirected to LL equivalents

HAL SPI define statements and enumerations are based on LL define statements.

Removal of hardware-related macros in HAL2

All hardware macros have been removed from the HAL SPI driver, and equivalent services are ensured through the LL APIs.

Revised and optimized HAL state machine

The SPI state machine is reworked to ensure transitions of the peripheral functional states.

Revised and optimized HAL API status returns

The HAL SPI functions were updated to return:

  • HAL_INVALID_PARAM: in case of a NULL p_config structure pointer.

  • HAL_ERROR: in case of an error.

  • HAL_BUSY: in case of an ongoing process.

  • HAL_OK: in case of a successfully completed operation.

Revised and optimized HAL error management

Redesign the error handling mechanisms within the HAL to enhance error detection, reporting, and management. This update aims to provide more detailed and consistent error information, such as HAL_SPI_ERROR_MODF and HAL_SPI_ERROR_FRE.

Replaced TypeDef suffix with _t in type names

All TypeDef parts of the HAL SPI driver have been removed and replaced by _t.

Replaced multi-line macros with inline functions

For the HAL SPI driver, large macros have been replaced by inline functions to maintain code consistency, improve readability, and benefit from compiler optimization. Inline functions also offer better support for debugging and strict type management, reducing the risk of errors.

Replaced nonmodifiable pointers with const keywords

Const keywords have been added to non-modifiable pointers to ensure that either the pointer itself or the data it points to cannot be modified. This enhances code safety and readability, prevents unintended modifications, and renders code more robust and maintainable.

Assertion implementation and usage in HAL1

The parameters and state are checked using ASSERT_DBG_STATE and ASSERT_DBG_PARAM macros.

Merge HAL extension files with common ones and remove the EX prefix from APIs.

Only one HAL SPI header file remains and all macros, define statements, and functions from hal_spi_ex.h have been moved to hal_spi.h.

Provide RTOS-compatible acquire/release bus services for communication peripheral drivers.

New functions, HAL_SPI_AcquireBus() and HAL_SPI_ReleaseBus(), are provided to allow the acquisition and release of a HAL SPI handle, protecting a full sequence.

Add HAL PPP Services for Drivers with Handle to Retrieve Corresponding Peripheral Clock Frequency.

The HAL SPI driver provides a new API to retrieve the clock frequency: HAL_SPI_GetClockFreq().

Replace Finite Parameter Defines (Including PPP Instances) with Enum Types at HAL Levels.

Finite parameters (including SPI instances) have been converted into enumerated types (enums) at HAL level. This improves code readability, maintainability, and type safety.

Add HAL_PPP Prefix to All HAL Peripheral APIs (Defines, Macros, Types, and Functions).

All HAL SPI functions start with HAL_SPI.